import { _decorator, Component, Node, Sprite, Animation, tween, Vec3, Color } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 特效基类
 * 
 * 功能：
 * 1. 管理特效的生命周期
 * 2. 控制特效的播放和停止
 * 3. 处理特效的参数调整
 * 4. 提供特效回收机制
 * 
 * 特点：
 * 1. 支持粒子系统
 * 2. 自动回收机制
 * 3. 参数动态调整
 * 4. 支持特效组合
 */
@ccclass('BaseEffect')
export abstract class BaseEffect extends Component {
    @property(Animation)
    protected animation: Animation | null = null;

    @property
    protected duration: number = 1;

    @property
    protected autoDestroy: boolean = true;

    protected isPlaying: boolean = false;

    start() {
        if (this.animation) {
            this.animation.on(Animation.EventType.FINISHED, this.onAnimationFinished, this);
        }
    }

    public play() {
        if (this.isPlaying) return;
        
        this.isPlaying = true;
        this.onEffectStart();

        if (this.animation) {
            this.animation.play();
        }

        if (this.autoDestroy) {
            this.scheduleOnce(() => {
                this.stop();
            }, this.duration);
        }
    }

    public stop() {
        if (!this.isPlaying) return;

        this.isPlaying = false;
        this.onEffectEnd();

        if (this.animation) {
            this.animation.stop();
        }

        if (this.autoDestroy) {
            this.node.destroy();
        }
    }

    protected onAnimationFinished() {
        if (this.autoDestroy) {
            this.stop();
        }
    }

    protected abstract onEffectStart(): void;
    protected abstract onEffectEnd(): void;

    onDestroy() {
        if (this.animation) {
            this.animation.off(Animation.EventType.FINISHED, this.onAnimationFinished, this);
        }
    }
} 